<?php
	/**
	 * 服务层基础类
	 */
	namespace Think\Service;
	abstract class BasicService{
		protected $db;
		private $cacheName;
		/**
		 * [__construct 初始化数据库连接]
		 * @Author   :seven
		 * @DateTime :2016-05-24T00:36:47+0800
		 * @Detail   :
		 * @param    [type]		$dbName [description]
		 */
		public function __construct($dbName) {
			$this->cacheName = str_replace("\\", "_", get_class($this));
			if(!isset($dbName) || empty($dbName)){
				$this->db = "Undefine";
			}else{
				if(is_string($dbName)){
					$this->db = M($dbName);
				}else{
					$this->db = M($dbName[0],$dbName[1]);
				}
			}
    	}

    	public function showDB(){
    		dump($this->db);
    	}

    	public function __call($method,$args) {
	       	$before_Action = "_before{$method}";
	       	$Action = ltrim($method, "_");;
	       	$after_Action = "_after{$method}";
	       	
	       	if(method_exists($this,$before_Action)){
	       		$before = $this->$before_Action();
	       	}
	       	/**如果是查询缓存得到结果**/
	       	if($before){
	       		return $before;
	       	}
	       	$res = $this->$Action($args);
	       	if($Action == 'insert' || $Action == 'update'
	       		|| $Action == 'remove' || $Action == 'find'){
	       		return $res;
	       	}
	       	 
	       	if(method_exists($this,$after_Action)){
	       		return $this->$after_Action($res);
	       	}
	    }
		/**
		 * [_before_insert 插入前置方法]
		 * @Author   :seven
		 * @DateTime :2016-06-08T16:10:19+0800
		 * @Detail   :删除缓存
		 */
		private function _before_insert(){
			S($this->cacheName,NULL);
			return false;
		}
		/**
		 * [insert 插入新元素]
		 * @Author   :seven
		 * @DateTime :2016-05-23T23:05:32+0800
		 * @Detail   :
		 */
		abstract public function insert($model);
		/**
		 * [_before_update description]
		 * @Author   :seven
		 * @DateTime :2016-06-08T16:26:01+0800
		 * @Detail   :
		 */
		private function _before_update(){
			S($this->cacheName,NULL);
			return false;
		}
		/**
		 * [update 更新]
		 * @Author   :seven
		 * @DateTime :2016-05-23T23:06:02+0800
		 * @Detail   :
		 */
		abstract public function update($model);
		/**
		 * [_before_remove description]
		 * @Author   :seven
		 * @DateTime :2016-06-08T16:26:18+0800
		 * @Detail   :
		 */
		private function _before_remove(){
			S($this->cacheName,NULL);
			return false;
		}
		/**
		 * [remove 删除]
		 * @Author   :seven
		 * @DateTime :2016-05-23T23:09:03+0800
		 * @Detail   :
		 */
		abstract public function remove($condition);
		/**
		 * [find 查询单个]
		 * @Author   :seven
		 * @DateTime :2016-06-08T13:39:12+0800
		 * @Detail   :
		 */
		abstract public function find($condition);
		/**
		 * [_before_query ]
		 * @Author   :seven
		 * @DateTime :2016-06-08T16:39:05+0800
		 * @Detail   :
		 */
		private function _before_query(){
			/*查询缓存*/
			return S($this->cacheName);
		}
		/**
		 * [query 查询多个]
		 * @Author   :seven
		 * @DateTime :2016-06-08T13:39:12+0800
		 * @Detail   :
		 */
		abstract public function query($condition);
		/**
		 * [_after_query ]
		 * @Author   :seven
		 * @DateTime :2016-06-08T16:34:30+0800
		 * @Detail   :
		 */
		private function _after_query($res){
			/*保存缓存*/
			S($this->cacheName,$res);
			return $res;
		}

		public function sql(){
			return $this->db->_sql();
		}
	} 
	?>